package com.chunxuan.langquan.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.UploadImageUrl;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.GlideUtil;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.support.view.ClearEditText;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import io.reactivex.functions.Consumer;

/**
 * 修改用户昵称
 */
public class EditUserNicknameActivity extends Base2Activity {
    private ImageView ivBack;
    private TextView tvSave;
    private ClearEditText etNickname;
    private String nickname;
    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_modify_user_nickname;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivBack=findViewById(R.id.menu_back);
        tvSave=findViewById(R.id.tv_save);
        etNickname=findViewById(R.id.et_nickname);
        Intent intent=getIntent();
        if(null!=intent){
            nickname=intent.getStringExtra("nickname");
            etNickname.setText(nickname);
        }
        setOnClickListener(ivBack,tvSave);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.menu_back:
                //设置返回的数据
                if(TextUtils.isEmpty(etNickname.getText())){
                    ToastUtils.showShort("昵称不能为空");
                    etNickname.setShakeAnimation();
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("new_nickname", etNickname.getText().toString().trim());
                    setResult(1002, intent);
                    //关闭当前activity
                    finish();
                }
                break;
            case R.id.tv_save:
                if(TextUtils.isEmpty(etNickname.getText())){
                    ToastUtils.showShort("昵称不能为空");
                    etNickname.setShakeAnimation();
                }else{
                    editUser(etNickname.getText().toString().trim());
                }
                break;
        }
    }

    private void editUser(String nickname) {
        APIRetrofit.getDefault().editUserNickname(Global.HEADER, nickname)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 result2) throws Exception {
                        hideProgress();
                        ToastUtils.showShort(result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("修改用户昵称失败");
                    }
                });
    }
}
